package bb.lanxing.model.database;

import android.content.ContentValues;
import android.database.Cursor;

import com.baidu.mapapi.model.LatLng;
import com.baidu.platform.comapi.map.MapBundleKey;
import com.garmin.fit.MonitoringReader;
//import com.orm.SugarRecord;
//import com.orm.dsl.Table;
//import com.umeng.umcrash.UMCrash;
import bb.lanxing.lib.devices.sprint.entity.SettingSupports;
import bb.lanxing.model.WorkoutContentProvider;
import bb.lanxing.model.WorkoutDatabaseHelper;
import bb.lanxing.util.CommonUtil;
import bb.lanxing.util.JsonUtil;
import bb.lanxing.util.Log;

import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.litepal.LitePal;


public class PacePoint extends LitePalTableBase {
    public static final int DISTANCE_1_KM = 1000;
    public static final float DISTANCE_FULL_MARATHON = 42195.0f;
    public static final float DISTANCE_HALF_MARATHON = 21097.5f;
    public static final int TYPE_NONE = 0;
    public static final int TYPE_EACH_KM = 1;
    public static final int TYPE_HALF_MARATHON = 2;
    public static final int TYPE_FULL_MARATHON = 3;
    public static final int TYPE_LAST_ODD = 4;
    private double altitude;
    private double distance;
    private long duration;
    private double latitude;
    private double longitude;
    private double maxSpeed;
    private int paceIndex;
    private int sport;
    private long timestamp;
    private int type;
    private String workoutUuid;


    public PacePoint() {
    }

    public PacePoint(JSONObject jSONObject) {
        this.paceIndex = JsonUtil.getIntegerValue("paceIndex", jSONObject);//MapBundleKey.MapObjKey.OBJ_SL_INDEX
        this.sport = JsonUtil.getIntegerValue("sport", jSONObject);//SettingSupports.SUPPORT_ITEM_SPORT
        this.type = JsonUtil.getIntegerValue("type", jSONObject);
        this.latitude = JsonUtil.getDoubleValue("latitude", jSONObject);
        this.longitude = JsonUtil.getDoubleValue("longitude", jSONObject);
        this.altitude = JsonUtil.getDoubleValue("altitude", jSONObject);
        this.timestamp = JsonUtil.getLongValue("timestamp", jSONObject);
        this.distance = JsonUtil.getDoubleValue("distance", jSONObject);//MonitoringReader.DISTANCE_STRING
        this.duration = JsonUtil.getLongValue("duration", jSONObject);
        this.maxSpeed = JsonUtil.getDoubleValue("maxSpeed", jSONObject);
    }

    public PacePoint(JSONObject jSONObject, IWorkout iWorkout) {
        this(jSONObject);
        this.workoutUuid = iWorkout.getUuid();
    }

    public JSONObject toJson() {
        JSONObject jSONObject;

        try {
            jSONObject = new JSONObject();

            jSONObject.put("paceIndex", this.paceIndex);//MapBundleKey.MapObjKey.OBJ_SL_INDEX
            jSONObject.put("sport", this.sport);//SettingSupports.SUPPORT_ITEM_SPORT
            jSONObject.put("type", this.type);
            jSONObject.put("timestamp", this.timestamp);
            jSONObject.put("duration", this.duration);
            jSONObject.put("latitude", CommonUtil.roundDouble(this.latitude, 6));
            jSONObject.put("longitude", CommonUtil.roundDouble(this.longitude, 6));
            jSONObject.put("altitude", CommonUtil.roundDouble(this.altitude, 2));
            jSONObject.put("distance", CommonUtil.roundDouble(this.distance, 2));//MonitoringReader.DISTANCE_STRING
            jSONObject.put("maxSpeed", CommonUtil.roundDouble(this.maxSpeed, 2));

        } catch (JSONException e3) {
            jSONObject = null;
            e3.printStackTrace();
        }

        return jSONObject;

    }

    public ContentValues toContentValues() {
        ContentValues contentValues = new ContentValues();
        if (this.id != null) {
            contentValues.put("id", this.id);
        }
        contentValues.put("workoutuuid", this.workoutUuid);
        contentValues.put("paceindex", this.paceIndex);
        contentValues.put("sport", this.sport);
        contentValues.put("type", this.type);
        contentValues.put("latitude", this.latitude);
        contentValues.put("longitude", this.longitude);
        contentValues.put("altitude", this.altitude);
        contentValues.put("timestamp", this.timestamp);
        contentValues.put("distance", this.distance);
        contentValues.put("duration", this.duration);
        contentValues.put("maxSpeed", this.maxSpeed);
        return contentValues;
    }

    public static PacePoint parseCursor(Cursor cursor) {
        return parseCursor(cursor, null);
    }

    public static PacePoint parseCursor(Cursor cursor, Map<String, Integer> map) {
        PacePoint pacePoint = new PacePoint();
        int index = getIndex(cursor, map, "id");
        if (index >= 0) {
            pacePoint.id = cursor.getLong(index);
        }
        int index2 = getIndex(cursor, map, "workoutuuid");
        if (index2 > 0) {
            pacePoint.workoutUuid = cursor.getString(index2);
        }
        int index3 = getIndex(cursor, map, "paceindex");
        if (index3 >= 0) {
            pacePoint.paceIndex = cursor.getInt(index3);
        }
        int index4 = getIndex(cursor, map, "sport");
        if (index4 >= 0) {
            pacePoint.sport = cursor.getInt(index4);
        }
        int index5 = getIndex(cursor, map, "type");
        if (index5 >= 0) {
            pacePoint.type = cursor.getInt(index5);
        }
        int index6 = getIndex(cursor, map, "latitude");
        if (index6 >= 0) {
            pacePoint.latitude = cursor.getDouble(index6);
        }
        int index7 = getIndex(cursor, map, "longitude");
        if (index7 >= 0) {
            pacePoint.longitude = cursor.getDouble(index7);
        }
        int index8 = getIndex(cursor, map, "altitude");
        if (index8 >= 0) {
            pacePoint.altitude = cursor.getDouble(index8);
        }
        int index9 = getIndex(cursor, map, "timestamp");
        if (index9 >= 0) {
            pacePoint.timestamp = cursor.getLong(index9);
        }
        int index10 = getIndex(cursor, map, "distance");
        if (index10 >= 0) {
            pacePoint.distance = cursor.getDouble(index10);
        }
        int index11 = getIndex(cursor, map, "duration");
        if (index11 >= 0) {
            pacePoint.duration = cursor.getLong(index11);
        }
        int index12 = getIndex(cursor, map, "maxspeed");
        if (index12 >= 0) {
            pacePoint.maxSpeed = cursor.getDouble(index12);
        }
        return pacePoint;
    }

    private static int getIndex(Cursor cursor, Map<String, Integer> map, String str) {
        if (map == null) {
            int columnIndex = cursor.getColumnIndex(str);
            if (columnIndex < 0) {
                columnIndex = cursor.getColumnIndex(str.toLowerCase());
            }
            return columnIndex < 0 ? cursor.getColumnIndex(str.toUpperCase()) : columnIndex;
        }
        Integer num = map.get(str);
        if (num != null) {
            return num;
        }
        return -1;
    }

    public String getPaceFormat() {
        return String.format("%d'%02d\"", this.duration / 60, this.duration % 60);
    }

//    @Override
//    @Deprecated
//    public long save() {
//        long save = super.save();
//        Log.i("db", "pace point [#" + save + "] saved ! workout: " + this.workoutUuid);
//        return save;
//    }

    public static String makeSampleKMInfoJsonString(String workoutUuid, int sample) {
        List<PacePoint> querySimplePacePoint = WorkoutDatabaseHelper.querySimplePacePoint(workoutUuid, sample);
        if (querySimplePacePoint == null || querySimplePacePoint.isEmpty()) {
            return null;
        }
        JSONArray jSONArray = new JSONArray();
        for (PacePoint pacePoint : querySimplePacePoint) {
            jSONArray.put(pacePoint.toJson());
        }
        return jSONArray.toString();
    }

    public static String makeKMInfoJsonString(String workoutUuid) {
        List<PacePoint> queryPacePoint = WorkoutDatabaseHelper.queryPacePoint(workoutUuid);
        if (queryPacePoint == null || queryPacePoint.isEmpty()) {
            return null;
        }
        JSONArray jSONArray = new JSONArray();
        for (PacePoint pacePoint : queryPacePoint) {
            jSONArray.put(pacePoint.toJson());
        }
        return jSONArray.toString();
    }

    public static List<PacePoint> getByWorkoutUuid(String workoutUuid) {
        return LitePal.where("workoutUuid =?", workoutUuid).find(PacePoint.class);
    }

    public void setPaceIndex(int paceIndex) {
        this.paceIndex = paceIndex;
    }

    public int getPaceIndex() {
        return paceIndex;
    }


    public String getWorkoutUuid() {
        return this.workoutUuid;
    }

    public void setWorkoutUuid(String workoutUuid) {
        this.workoutUuid = workoutUuid;
    }

    public int getSport() {
        return this.sport;
    }

    public void setSport(int sport) {
        this.sport = sport;
    }

    public int getIndex() {
        return this.paceIndex;
    }

    public void setIndex(int index) {
        this.paceIndex = index;
    }

    public int getType() {
        return this.type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public double getLatitude() {
        return this.latitude;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public double getLongitude() {
        return this.longitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }

    public LatLng getLatLng() {
        return new LatLng(this.latitude, this.longitude);
    }

    public double getAltitude() {
        return this.altitude;
    }

    public void setAltitude(double altitude) {
        this.altitude = altitude;
    }

    public long getTimestamp() {
        return this.timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public double getDistance() {
        return this.distance;
    }

    public void setDistance(double distance) {
        this.distance = distance;
    }

    public long getDuration() {
        return this.duration;
    }

    public void setDuration(long duration) {
        this.duration = duration;
    }

    public double getMaxSpeed() {
        return this.maxSpeed;
    }

    public void setMaxSpeed(double maxSpeed) {
        this.maxSpeed = maxSpeed;
    }
}